(function(){
    var adcode=''
    /*
    * 定位
    * 先浏览器定位，后采用IP定位
    * */
    function geolocation (){
        AMap.plugin('AMap.Geolocation', function() {
            var geolocation = new AMap.Geolocation({
                // 是否使用高精度定位，默认：true
                enableHighAccuracy: true,
                // 设置定位超时时间，默认：无穷大
                timeout: 10000,
                // 定位按钮的停靠位置的偏移量，默认：Pixel(10, 20)
                buttonOffset: new AMap.Pixel(10, 20),
                //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
                zoomToAccuracy: true,
                //  定位按钮的排放位置,  RB表示右下
                buttonPosition: 'RB'
            })

            geolocation.getCurrentPosition()
            AMap.event.addListener(geolocation, 'complete', onComplete)
            AMap.event.addListener(geolocation, 'error', onError)

            function onComplete (data) {
                // data是具体的定位信息
                adcode= data.addressComponent.adcode
            }

            function onError (data) {
                // 定位出错
                console.log(data)
            }
        })
    }
    function citySearch(){
        var citysearch = new AMap.CitySearch();
        //自动获取用户IP，返回当前城市
        citysearch.getLocalCity(function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                adcode = result.adcode;
            } else {

            }
        });
    }
    geolocation()



    if(/(\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3})/g.test(window.location.href)){
        //window.base='';
    }
    var gift=jck.ajax;
    //获取手机验证码
    $('#getCode').click(function(){
        var t=$(this)
        if(!!t.data('disable')){
            return ;
        }
        var mobile=$('#mobile').val();
        if(!/^((\(\d{2,3}\))|(\d{3}\-))?1(3|4|5|6|7|8)\d{9}$/.test(mobile)){
            layer.msg('请输入正确的手机号');
            return;
        }
        t.data('disable',true)
        t.data('No.',60)
        t.data('setInputValue',againGetCode())
        var p={mobile:mobile}
        gift.ajax_md5({
            url:base+'/api/v1/common/identifyCode/send',
            params:p,
            successCallBack:function(data){layer.msg('发送成功')},
            errorCallBack:function(data){
                // t.data('disable',false)
                layer.msg(data.msg || '发送失败')
                // t.data('No.',0)
            }
        });
    })
    //重新获取验证码倒计时
    function againGetCode(){
        //setInterval()
        return setInterval(function(){
            var t=$('#getCode'),no=t.data('No.');
            if(no>0){
                t.html(no+'s');
                no--;
                t.data('No.',no)
            }else{
                t.html('重新发送')
                t.data('disable',false)
                clearInterval( t.data('setInputValue'))
            }
        },1000)
    }
    //提交
    $('#sub-btn').click(function(){
        var mobile=$('#mobile').val(),identifyCode=$('#identifyCode').val(),giftUuid=$('#giftUuid').val(),cid=$('#cid').val();
        if(!mobile || !identifyCode){
            layer.msg('请输入手机号或验证码数据');
            return
        }
        if(!giftUuid){
            layer.msg('请检查URL是否有giftUuid参数！');
            return;
        }
        /*if(!cid){
            layer.msg('请检查URL是否有cid参数！');
            return;
        }*/
        if(!/^((\(\d{2,3}\))|(\d{3}\-))?1(3|4|5|6|7|8)\d{9}$/.test(mobile)){
            layer.msg('请输入正确的手机号');
            return;
        }
        gift.ajax_md5({
            url:base+'/api/v1/passenger/drawQrCodeGift',
            params:{
                mobile:mobile,
                identifyCode:identifyCode,
                giftUuid:giftUuid,
                cid:cid,
                areacode:adcode
            },
            successCallBack:function(data){
                //alert('领取成功');
               $('#getSuccess').show();
                $('.mark').show();
                $('.couponMoney').html(data.data.couponMoney)
            },
            errorCallBack:function(data){
                if(data.errCode==60007){
                    $('#oldlong').show()
                    $('#colse_long').css({
                        'background-color': '#fff',
                        'border': '1px solid #4f5a67',
                        'color': '#4f5a67'
                    })
                    $('.active').css('background',' #eee')
                }else{
                    $('#oldlong').hide()
                    $('#colse_long').attr('style','');
                }
                $('#tongtan').find('.textt').html(data.msg);
                $('#tongtan').show();
                $('.mark').show();

            }
        });
    });
    //显示规则
    $('#showRule').click(function(){
        $('#guizetan').show();
        $('.mark').show();
    });
    /*关闭弹窗*/
    $('.gift-tan').on('click','.colse_tan',function(){
        $(this).parents('.gift-tan').hide();
        $('.mark').hide();
    })
})()